/*
 * @Description: 全宗上下文
 * @Author: Rfan
 * @Date: 2022-01-08 16:30:05
 * @LastEditTime: 2023-02-27 09:26:56
 */

import { v4 as uuidv4 } from 'uuid';
import { createContext, useContext, useEffect, useReducer } from 'react';
import {
  ADD_CONDITION_ITEM,
  ADD_SCOPE_ITEM,
  CLEAR_CONDITION_LIST,
  DELETE_LAST_CONDITION_ITEM,
  DELETE_SCOPE_ITEM,
  SET_FIELD_LIST,
  SET_FOND_LIST,
  SET_DB_NAME
} from '../constants';
import { AdvanceReducer } from '../reducers/AdvanceReducer';
import { fetchFondsListByUnitId } from '@/api/fonds';
import { searchFetchFields } from '@/api/metadata';
import { appState } from '@/store/mobx';

class ScopeItemClass {
  id;

  fondId = '';

  atTypeId = '';

  atCode = '';

  atType = '';

  dbName = 'data';

  levelCode = [];

  constructor() {
    this.id = uuidv4();
  }

  generateData() {
    return {
      fondsId: this.fondId,
      archiveTypeId: this.atTypeId,
      atType: this.atType,
      atCode: this.atCode,
      level: this.levelCode
    };
  }
}

const fieldList = [
  { field_desc: '全宗号', field_name: 'FONDCODE', field_type: 0 },
  { field_desc: '流水号', field_name: 'SERIALNO', field_type: 1 },
  { field_desc: '档号', field_name: 'ARCNO', field_type: 0 },
  { field_desc: '题名', field_name: 'TITLE', field_type: 0 }
];

const initialState = {
  leftCol: 20,
  fondsList: [],
  scopeList: [new ScopeItemClass()],
  fieldList,
  conditionList: []
};

const AdvanceContext = createContext();

const AdvanceContextProvider = (props) => {
  const [state, dispatch] = useReducer(AdvanceReducer, initialState);

  const actions = {
    async getFondsList() {
      const res = await fetchFondsListByUnitId(appState?.unitId);
      const { data } = res;
      dispatch({ type: SET_FOND_LIST, data });
    },
    /* dbName选择 */
    setDbName(data) {
      dispatch({ type: SET_DB_NAME, data });
    },
    /**
     * 增加检索范围
     */
    addScopeItem(id) {
      dispatch({ type: ADD_SCOPE_ITEM, id, data: new ScopeItemClass() });
    },

    /**
     * 删除检索范围
     */
    deleteScopeItem(id) {
      dispatch({ type: DELETE_SCOPE_ITEM, id });
    },

    /**
     * 获取field选项
     */
    getFieldList() {
      console.log('get');
      const query = state.scopeList.map((item) => item.generateData());
      searchFetchFields(query).then((res) => {
        console.log('fidles', res.data);
        const { data } = res;
        dispatch({ type: SET_FIELD_LIST, data });
      });
    },

    addConditionItem(data) {
      dispatch({ type: ADD_CONDITION_ITEM, data });
    },

    deleteLastConditionItem(data) {
      dispatch({ type: DELETE_LAST_CONDITION_ITEM, data });
    },

    clearConditionList() {
      dispatch({ type: CLEAR_CONDITION_LIST });
    }
  };

  useEffect(() => {
    actions.getFondsList();
  }, []);

  return (
    <AdvanceContext.Provider value={{ ...state, ...actions }}>
      {props.children}
    </AdvanceContext.Provider>
  );
};

export const useAdvanceContext = () => {
  return useContext(AdvanceContext);
};

export { AdvanceContext, AdvanceContextProvider };
